package com.siyoumi.app.modules.sh.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siyoumi.app.entity.ShFund;
import com.siyoumi.app.modules.sh.entity.EnumShFundType;
import com.siyoumi.app.modules.sh.service.SvcShFund;
import com.siyoumi.app.modules.sh.vo.ShFundData;
import com.siyoumi.component.XApp;
import com.siyoumi.component.XEnumBase;
import com.siyoumi.component.http.InputData;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.util.XReturn;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/xadmin/sh/sh__fund__list")
public class sh__fund__list
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        setPageTitle("基金-列表");

        InputData inputData = InputData.fromRequest();
        JoinWrapperPlus<ShFund> query = SvcShFund.getBean().listQuery();

        IPage<ShFund> page = new Page<>(inputData.getPageIndex(), 100);
        //list
        IPage<Map<String, Object>> pageData = SvcShFund.getApp().getMaps(page, query);
        List<Map<String, Object>> list = pageData.getRecords();
        long count = pageData.getTotal();

        //计算总金额
        BigDecimal buyPriceTotal = BigDecimal.ZERO;
        BigDecimal buyPriceCore = BigDecimal.ZERO; //核销金额 40%
        BigDecimal buyPriceHigh = BigDecimal.ZERO; //卫星金额 60%
        for (Map<String, Object> item : list) {
            ShFund entity = SvcShFund.getApp().loadEntity(item);
            ShFundData data = entity.fundData();

            buyPriceTotal = buyPriceTotal.add(data.getBuy_price());
            if ("core".equals(data.getType())) {
                buyPriceCore = buyPriceCore.add(data.getBuy_price());
            } else {
                buyPriceHigh = buyPriceHigh.add(data.getBuy_price());
            }
        }

        setPageInfo("buy_price_total", buyPriceTotal);
        setPageInfo("buy_price_core_set", XApp.toDecimalPay(buyPriceTotal.multiply(BigDecimal.valueOf(0.6)))); //核心最多投入金额
        setPageInfo("buy_price_high_set", XApp.toDecimalPay(buyPriceTotal.multiply(BigDecimal.valueOf(0.4)))); //卫星最多投入金额
        setPageInfo("buy_price_core", buyPriceCore);
        setPageInfo("buy_price_high", buyPriceHigh);

        EnumShFundType enumShFundType = XEnumBase.of(EnumShFundType.class);//类型
        for (Map<String, Object> item : list) {
            ShFund entity = SvcShFund.getApp().loadEntity(item);
            ShFundData data = entity.fundData();

            item.put("id", entity.getKey());
            item.put("data", data);
            item.put("type", enumShFundType.get(data.getType()));
            //金额占比
            BigDecimal prizeRate = BigDecimal.ZERO;
            if (buyPriceTotal.compareTo(BigDecimal.ZERO) > 0) {
                prizeRate = data.getBuy_price().divide(buyPriceTotal, 3, RoundingMode.DOWN);
            }
            Boolean prizeHot = false; //单个基金金额已超15%
            if (prizeRate.compareTo(BigDecimal.valueOf(0.15)) > 0) {
                prizeHot = true;
            }
            item.put("prize_rate", prizeRate);
            item.put("prize_hot", prizeHot);
        }

        getR().setData("list", list);
        getR().setData("count", count);

        return getR();
    }


    //删除
    @Transactional
    @PostMapping("/del")
    public XReturn del() {
        return SvcShFund.getBean().delete(getIds());
    }
}
